package com.why.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.why.entity.Courseselection;
import com.why.entity.Orders;
import com.why.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * (Orders)表控制层
 *
 * @author makejava
 * @since 2023-05-23 20:24:52
 */
@RestController
@RequestMapping("/orders")
public class OrdersController {
    /**
     * 服务对象
     */
    @Autowired
    private OrdersService ordersService;

    @PostMapping("/save")
    public String save(@RequestBody Orders orders) {
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<Orders>();
//        如果同一个学生购买同一个课程则购买失败
        queryWrapper.eq(Orders::getCourseid, orders.getCourseid()).eq(Orders::getStudentid, orders.getStudentid());
        Orders one = ordersService.getOne(queryWrapper);
        if (orders.getStudentid() == null || orders.getStudentname() == null) {
            return "no";
        } else if (one != null) {
            return "exist";
        } else {
            ordersService.save(orders);
            return "success";
        }
    }

    @DeleteMapping("/delete/{id}")
    public String delete(@PathVariable("id") Long id) {
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<Orders>();
        queryWrapper.eq(Orders::getId, id);
        Orders one = ordersService.getOne(queryWrapper);
        if (one != null) {
            ordersService.removeById(id);
            return "success";
        } else {
            return "no";
        }
    }

    @GetMapping("/page")
    private Page<Orders> page(int page, int pageSize, String name) {
        Page pageInfo = new Page(page, pageSize);
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<Orders>();
        queryWrapper.like(Orders::getCoursename, name);
        queryWrapper.orderByDesc(Orders::getAddtime);
        ordersService.page(pageInfo, queryWrapper);
        return pageInfo;

    }

}

